package com.geekai.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geekai.chat.dto.ConversationDto;
import com.geekai.chat.dto.MessageDto;
import com.geekai.chat.entity.Conversation;
import com.geekai.chat.entity.User;
import com.geekai.chat.mapper.ConversationMapper;
import com.geekai.chat.service.ConversationService;
import com.geekai.chat.service.MessageService;
import com.geekai.chat.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Conversation> implements ConversationService {

    private final UserService userService;
    private final MessageService messageService;

    @Override
    public Conversation createConversation(String username, String title) {
        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        Conversation conversation = new Conversation();
        conversation.setUserId(user.getId());
        conversation.setTitle(title != null ? title : "新对话");
        conversation.setModel("gpt-3.5-turbo");
        conversation.setStatus(1);

        save(conversation);
        return conversation;
    }

    @Override
    public List<ConversationDto> getUserConversations(String username) {
        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        QueryWrapper<Conversation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId())
                .eq("status", 1)
                .orderByDesc("update_time");

        List<Conversation> conversations = list(wrapper);
        return conversations.stream().map(conversation -> {
            ConversationDto dto = new ConversationDto();
            BeanUtils.copyProperties(conversation, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public ConversationDto getConversationWithMessages(Long conversationId, String username) {
        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        Conversation conversation = getById(conversationId);
        if (conversation == null || !conversation.getUserId().equals(user.getId())) {
            throw new RuntimeException("对话不存在或无权限访问");
        }

        ConversationDto dto = new ConversationDto();
        BeanUtils.copyProperties(conversation, dto);

        List<MessageDto> messages = messageService.getConversationMessages(conversationId);
        dto.setMessages(messages);

        return dto;
    }

    @Override
    public void deleteConversation(Long conversationId, String username) {
        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        Conversation conversation = getById(conversationId);
        if (conversation == null || !conversation.getUserId().equals(user.getId())) {
            throw new RuntimeException("对话不存在或无权限访问");
        }

        removeById(conversationId);
    }

    @Override
    public void updateConversationTitle(Long conversationId, String title, String username) {
        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        Conversation conversation = getById(conversationId);
        if (conversation == null || !conversation.getUserId().equals(user.getId())) {
            throw new RuntimeException("对话不存在或无权限访问");
        }

        conversation.setTitle(title);
        updateById(conversation);
    }
}